package com.dflc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dflc.common.CommonConstant;
import com.dflc.entity.po.TBackLoginUser;
import com.dflc.entity.rep.HttpResponseEntity;
import com.dflc.entity.req.LogPageReq;
import com.dflc.entity.vo.TLogVo;
import com.dflc.mapper.TLogMapper;
import com.dflc.service.TBackLoginUserService;
import com.dflc.service.TLogService;
import com.dflc.utils.EntityUtils;
import com.dflc.utils.PageEntity;
import com.dflc.utils.PageEntityBuilder;
import com.dflc.utils.PageRequestEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 后台操作日志登陆日志
 *
 * @author tqx
 * @version 1.0.0.0
 * <p>Title: LogController </p >
 * <p>Description: 后台操作日志登陆日志  </p >
 * Package: com.dlfc.controller
 * Date: 2025年01月7日 16:40
 */
@RestController
@Slf4j
@RequestMapping("/log")
public class LogController {

    @Resource
    public TLogService tLogService;

    @Resource
    public TBackLoginUserService tBackLoginUserService;

    @Resource
    public TLogMapper tLogMapper;

    /**
     * 分页查询操作日志
     * @return HttpResponseEntity
     * @author tqx
     * @date 2025/01/07 17:14
     * Description:[getLogByPage]
     */
    @PostMapping("/getLogByPage")
    public HttpResponseEntity getLogByPage(@RequestBody PageRequestEntity page) {
        try{
            Integer id3 = StpUtil.getLoginIdAsInt();
            TBackLoginUser sysLoginUser = tBackLoginUserService.getById(id3);
            if (sysLoginUser == null) {
                return HttpResponseEntity.error(500, "查询不到当前用户的信息！请检查数据！");
            }else if (sysLoginUser.getState()==CommonConstant.StateActive.Deleted) {
                return HttpResponseEntity.error(401, "当前用户已被删除！");
            }
            LogPageReq req = new LogPageReq();
            req.setCurrent(page.getCurrentPage());
            req.setSize(page.getPageSize());
            if (!ObjectUtils.isEmpty(page.getSort()) && page.getSort().get(0).getType()!=null){
                req.setOrders(EntityUtils.sort(page));
            }
            if (!ObjectUtils.isEmpty(page.getQuery().get("operation"))){
                req.setOperation(page.getQuery().get("operation").toString());
            }
            if (!ObjectUtils.isEmpty(page.getQuery().get("description"))){
                req.setDescription(page.getQuery().get("description").toString());
            }
            if (!ObjectUtils.isEmpty(page.getQuery().get("userName"))){
                req.setUserName(page.getQuery().get("userName").toString());
            }
            if (!ObjectUtils.isEmpty(page.getQuery().get("timestampStar"))){
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date =dateFormat.parse(page.getQuery().get("timestampStar").toString());
                req.setTimestampStar(date);
            }
            if (!ObjectUtils.isEmpty(page.getQuery().get("timestampEnd"))){
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date =dateFormat.parse(page.getQuery().get("timestampEnd").toString());
                req.setTimestampEnd(date);
            }
            if (!ObjectUtils.isEmpty(page.getQuery().get("userId"))){
                req.setUserId(Integer.valueOf(page.getQuery().get("userId").toString()));
            }
            if (!ObjectUtils.isEmpty(page.getQuery().get("type"))){
                req.setType(Integer.valueOf(page.getQuery().get("type").toString()));
            }
            if (!ObjectUtils.isEmpty(page.getQuery().get("unitId"))){
                String str = page.getQuery().get("unitId").toString();
                List<String> list = Arrays.asList(str.split(","));
                List<Integer> list1 = new ArrayList<>();
                for (String s : list) {
                    list1.add(Integer.valueOf(s));
                }
                req.setUnitIds(list1);
            }
            Page<TLogVo> page1 = tLogMapper.pageQuery(req);
            PageEntity entity = PageEntityBuilder.builder()
                    .data(page1.getRecords())
                    .currentPage(page1.getCurrent())
                    .pageSize(page1.getSize())
                    .totalCount(page1.getTotal())
                    .totalPage(page1.getPages())
                    .build();
            return HttpResponseEntity.success(entity);
        }catch (Exception e){
            return HttpResponseEntity.error(500, e.getMessage());
        }
    }
}
